package com.example.homework

import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDelegate
import com.example.homework.databinding.ActivityMainBinding
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.cancelChildren
import kotlinx.coroutines.delay
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch


class MainActivity : AppCompatActivity() {
    private lateinit var binding: ActivityMainBinding
    private var timerCount: Int = 5
    private var isRunning: Boolean = false
    private val scope: CoroutineScope = CoroutineScope(Dispatchers.Main + Job())
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
//        binding.changeTheme.isChecked =
//            AppCompatDelegate.getDefaultNightMode() == AppCompatDelegate.MODE_NIGHT_YES
        binding.changeTheme.setOnCheckedChangeListener { compoundButton, b ->
            if (b) {
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
            } else {
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
            }
        }
        binding.countDownSlider.addOnChangeListener { _, value, _ ->
            timerCount = value.toInt()
            binding.countDownProgressBar.max = timerCount
            drawTick();
        }
        binding.countDownStart.setOnClickListener {
            if (isRunning) {
                isRunning = false
                scope.coroutineContext.cancelChildren()
                showMessage("Timer task canceled")
                default()
            } else {
                isRunning = true
                binding.countDownStart.text = getString(R.string.countDownStop)
                binding.countDownSlider.isEnabled = false
                binding.changeTheme.isEnabled = false
                start()
            }
        }
    }

    private fun start() {
        scope.launch {
            if (scope.isActive) {
                while (timerCount > 0) {
                    timerCount--
                    drawTick()
                    delay(1000)
                }
                showMessage("Timer task finished")
                default()
            }
        }
    }

    private fun showMessage(message: String) {
        Toast.makeText(baseContext, message, Toast.LENGTH_SHORT).show()
    }

    private fun default() {
        isRunning = false
        timerCount = 5
        binding.countDownSlider.isEnabled = true
        binding.changeTheme.isEnabled = true
        binding.countDownSlider.value = 5f
        binding.countDownProgressBar.max = 5
        binding.countDownProgressBar.progress = 5
        binding.countDownText.text = "5"
        binding.countDownStart.text = getString(R.string.countDownStart)
    }

    private fun drawTick() {
        binding.countDownText.text = "$timerCount"
        binding.countDownProgressBar.progress = timerCount
    }
}